//
//  WmcImageTailorFrameView.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/6/17.
//

import UIKit
import YPBedrock

extension WmcImageTailorFrameView {
    enum MoveLocation {
        case topLeft, topCenter, topRight
        case centerLeft, centerRight
        case bottomLeft, bottomCenter, bottomRight
    }
}

// MARK: - [WmcImageTailorFrameView]

class WmcImageTailorFrameView: WmcBaseView {
    /// 虚线框
    lazy var dottedLayer: CAShapeLayer = {
        let layer = CAShapeLayer()
        layer.fillColor = UIColor.clear.cgColor
        layer.strokeColor = UIColor.white.cgColor
        layer.lineWidth = 2
        layer.lineJoin = .round
        layer.lineDashPattern = [NSNumber(value: 5), NSNumber(value: 3)]
        layer.shadowColor = UIColor.wmc_grayH5_606066.cgColor
        layer.shadowRadius = 4
        layer.shadowOffset = CGSize(width: 0, height: 1)
        layer.shadowOpacity = 0.8
        return layer
    }()

    lazy var topLeftControlView: WmcImageTailorPanControlView = {
        let view = WmcImageTailorPanControlView(image: "tailor_frame_top_left".wmc_image,
                                                edge: .init(top: hvEdge, left: hvEdge, bottom: 0, right: 0))
        return view
    }()

    lazy var topCenterControlView: WmcImageTailorPanControlView = {
        let view = WmcImageTailorPanControlView(image: "tailor_frame_horizontal".wmc_image,
                                                edge: .init(top: hvEdge, left: hvLineEdge, bottom: hvEdge, right: hvLineEdge))
        return view
    }()

    lazy var topRightControlView: WmcImageTailorPanControlView = {
        let view = WmcImageTailorPanControlView(image: "tailor_frame_top_right".wmc_image,
                                                edge: .init(top: hvEdge, left: 0, bottom: 0, right: hvEdge))
        return view
    }()

    lazy var centerLeftControlView: WmcImageTailorPanControlView = {
        let view = WmcImageTailorPanControlView(image: "tailor_frame_vertical".wmc_image,
                                                edge: .init(top: hvLineEdge, left: hvEdge, bottom: hvLineEdge, right: hvEdge))
        return view
    }()

    lazy var centerRightControlView: WmcImageTailorPanControlView = {
        let view = WmcImageTailorPanControlView(image: "tailor_frame_vertical".wmc_image,
                                                edge: .init(top: hvLineEdge, left: hvEdge, bottom: hvLineEdge, right: hvEdge))
        return view
    }()

    lazy var bottomLeftControlView: WmcImageTailorPanControlView = {
        let view = WmcImageTailorPanControlView(image: "tailor_frame_bottom_left".wmc_image,
                                                edge: .init(top: 0, left: hvEdge, bottom: hvEdge, right: 0))
        return view
    }()

    lazy var bottomCenterControlView: WmcImageTailorPanControlView = {
        let view = WmcImageTailorPanControlView(image: "tailor_frame_horizontal".wmc_image,
                                                edge: .init(top: hvEdge, left: hvLineEdge, bottom: hvEdge, right: hvLineEdge))
        return view
    }()

    lazy var bottomRightControlView: WmcImageTailorPanControlView = {
        let view = WmcImageTailorPanControlView(image: "tailor_frame_bottom_right".wmc_image,
                                                edge: .init(top: 0, left: 0, bottom: hvEdge, right: hvEdge))
        return view
    }()

    /// 移动回调
    var touchPanBlock: YPBlock.TwoParam<MoveLocation, CGPoint>?

    let controlSize: CGFloat = 54

    let controlLineWidth: CGFloat = 5
    
    var hvEdge: CGFloat {
        return (controlSize - controlLineWidth) / 2
    }
    
    var hvLineEdge: CGFloat {
        return (controlSize - 34) / 2
    }

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        for view in subviews {
            if view.frame.contains(point) {
                return view
            }
        }
        return nil
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        let margin = controlSize / 2
        let offset = 2 * margin
        CATransaction.begin()
        CATransaction.setDisableActions(true)
        dottedLayer.frame = .init(x: margin,
                                  y: margin,
                                  width: frame.size.width - offset,
                                  height: frame.size.height - offset)
        let path = UIBezierPath(roundedRect: dottedLayer.bounds, cornerRadius: 0)
        dottedLayer.path = path.cgPath
        layer.insertSublayer(dottedLayer, at: 0)
        CATransaction.commit()
    }

    override func makeUI() {
        backgroundColor = nil

        addSubview(topLeftControlView)
        topLeftControlView.snp.makeConstraints { make in
            make.top.left.equalToSuperview()
            make.size.equalTo(controlSize)
        }
        addSubview(topCenterControlView)
        topCenterControlView.snp.makeConstraints { make in
            make.top.centerX.equalToSuperview()
            make.size.equalTo(controlSize)
        }
        addSubview(topRightControlView)
        topRightControlView.snp.makeConstraints { make in
            make.top.right.equalToSuperview()
            make.size.equalTo(controlSize)
        }

        addSubview(centerLeftControlView)
        centerLeftControlView.snp.makeConstraints { make in
            make.left.centerY.equalToSuperview()
            make.size.equalTo(controlSize)
        }
        addSubview(centerRightControlView)
        centerRightControlView.snp.makeConstraints { make in
            make.centerY.right.equalToSuperview()
            make.size.equalTo(controlSize)
        }

        addSubview(bottomLeftControlView)
        bottomLeftControlView.snp.makeConstraints { make in
            make.bottom.left.equalToSuperview()
            make.size.equalTo(controlSize)
        }
        addSubview(bottomCenterControlView)
        bottomCenterControlView.snp.makeConstraints { make in
            make.bottom.centerX.equalToSuperview()
            make.size.equalTo(controlSize)
        }
        addSubview(bottomRightControlView)
        bottomRightControlView.snp.makeConstraints { make in
            make.bottom.right.equalToSuperview()
            make.size.equalTo(controlSize)
        }
    }

    override func bindViewAction() {
        let topLeftPan = UIPanGestureRecognizer.YP.Create { [weak self] pan in
            guard let weakSelf = self else { return }
            let translation = pan.translation(in: pan.view)
            weakSelf.touchPanBlock?(.topLeft, translation)
            pan.setTranslation(.zero, in: pan.view) // 归零
        }
        topLeftControlView.addGestureRecognizer(topLeftPan)

        let topCenterPan = UIPanGestureRecognizer.YP.Create { [weak self] pan in
            guard let weakSelf = self else { return }
            let translation = pan.translation(in: pan.view)
            weakSelf.touchPanBlock?(.topCenter, translation)
            pan.setTranslation(.zero, in: pan.view) // 归零
        }
        topCenterControlView.addGestureRecognizer(topCenterPan)

        let topRightPan = UIPanGestureRecognizer.YP.Create { [weak self] pan in
            guard let weakSelf = self else { return }
            let translation = pan.translation(in: pan.view)
            weakSelf.touchPanBlock?(.topRight, translation)
            pan.setTranslation(.zero, in: pan.view) // 归零
        }
        topRightControlView.addGestureRecognizer(topRightPan)

        let centerLeftPan = UIPanGestureRecognizer.YP.Create { [weak self] pan in
            guard let weakSelf = self else { return }
            let translation = pan.translation(in: pan.view)
            weakSelf.touchPanBlock?(.centerLeft, translation)
            pan.setTranslation(.zero, in: pan.view) // 归零
        }
        centerLeftControlView.addGestureRecognizer(centerLeftPan)

        let centerRightPan = UIPanGestureRecognizer.YP.Create { [weak self] pan in
            guard let weakSelf = self else { return }
            let translation = pan.translation(in: pan.view)
            weakSelf.touchPanBlock?(.centerRight, translation)
            pan.setTranslation(.zero, in: pan.view) // 归零
        }
        centerRightControlView.addGestureRecognizer(centerRightPan)

        let bottomLeftPan = UIPanGestureRecognizer.YP.Create { [weak self] pan in
            guard let weakSelf = self else { return }
            let translation = pan.translation(in: pan.view)
            weakSelf.touchPanBlock?(.bottomLeft, translation)
            pan.setTranslation(.zero, in: pan.view) // 归零
        }
        bottomLeftControlView.addGestureRecognizer(bottomLeftPan)

        let bottomCenterPan = UIPanGestureRecognizer.YP.Create { [weak self] pan in
            guard let weakSelf = self else { return }
            let translation = pan.translation(in: pan.view)
            weakSelf.touchPanBlock?(.bottomCenter, translation)
            pan.setTranslation(.zero, in: pan.view) // 归零
        }
        bottomCenterControlView.addGestureRecognizer(bottomCenterPan)

        let bottomRightPan = UIPanGestureRecognizer.YP.Create { [weak self] pan in
            guard let weakSelf = self else { return }
            let translation = pan.translation(in: pan.view)
            weakSelf.touchPanBlock?(.bottomRight, translation)
            pan.setTranslation(.zero, in: pan.view) // 归零
        }
        bottomRightControlView.addGestureRecognizer(bottomRightPan)
    }
}
